
<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import "./echarts-auto-tooltip";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    chartData: {
      type: Number,
      required: true,
      default: 100,
    },
  },
  data() {
    return {
      chart: null,
      timer: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      let option = {
        // backgroundColor: "#354687",
        title: [
          {
            text: "当班在线率",
            x: "center",
            top: "32%",
            textStyle: {
              color: "#fff",
              fontSize: 14,
              fontWeight: "100",
            },
          },
          {
            text: chartData + "%",
            x: "center",
            top: "50%",
            textStyle: {
              fontSize: 14,
              color: "#00ffc0",
              fontWeight: "500",
            },
          },
        ],
        polar: {
          radius: ["44%", "50%"],
          center: ["50%", "50%"],
        },
        angleAxis: {
          max: 100,
          show: false,
        },
        radiusAxis: {
          type: "category",
          show: true,
          axisLabel: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },
        series: [
          {
            type: "pie",
            startAngle: 0,
            radius: ["78%", "85%"],
            center: ["50%", "50%"],
            data: [
              {
                hoverOffset: 1,
                value: chartData,
                name: "",
                itemStyle: {
                  normal: {
                    color: "#00ffc0",
                  },
                },
                label: {
                  show: false,
                },
                labelLine: {
                  normal: {
                    smooth: true,
                    lineStyle: {
                      width: 0,
                    },
                  },
                },
                hoverAnimation: false,
              },
              {
                label: {
                  show: false,
                },
                labelLine: {
                  normal: {
                    smooth: true,
                    lineStyle: {
                      width: 0,
                    },
                  },
                },
                value: 100 - chartData,
                hoverAnimation: false,
                itemStyle: {
                  color: "rgba(251, 200, 79, 0)",
                },
              },
            ],
          },
          {
            name: "",
            type: "pie",
            startAngle: 90,
            radius: "80%",
            hoverAnimation: false,
            center: ["50%", "50%"],
            itemStyle: {
              normal: {
                labelLine: {
                  show: false,
                },
                color: new echarts.graphic.RadialGradient(0.5, 0.5, 1, [
                  {
                    offset: 1,
                    color: "rgba(55,70,130, 1)",
                  },
                  {
                    offset: 0,
                    color: "rgba(55,70,130, 0)",
                  },
                ]),
                shadowBlur: 10,
                // shadowColor: 'rgba(55,70,130, 1)'
              },
            },
            data: [
              {
                value: 100,
              },
            ],
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>
